package frames;

import utils.PropertiesUtil;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @version V1.0
 * @Title: 新增备忘的frame
 * @Package
 * @Description:
 * @author: GaleBlade
 * @date:
 */
public class AddFrame extends JFrame{

    public JTextArea textArea = new JTextArea(12,25);
    public PropertiesUtil dataCache = new PropertiesUtil("DATA.properties");
    public Font font = new Font("微软雅黑", Font.BOLD, 24);
    //用来区分新增和更新
    public Boolean isUpdate = false;
    public int signal = 0;

    public AddFrame(){
        //内容面板
        JPanel contentPane = (JPanel) getContentPane();
        //设置窗格透明
        contentPane.setOpaque(false);
        contentPane.setLayout(null);
        //初始化文本编辑框
        JLabel areaBg = new JLabel();
        areaBg.setIcon(new ImageIcon("resources/images/label.png"));
        areaBg.setBounds(12,12,560,360);
        textArea.setFont(font);
        textArea.setLineWrap(true);
        textArea.setForeground(Color.WHITE);
        textArea.setBounds(12,12,560,360);
        textArea.setOpaque(false);
        //初始化保存按钮
        JButton save = new JButton("保存");
        save.setIcon(new ImageIcon("resources/images/label.png"));
        save.setFont(font);
        save.setForeground(Color.WHITE);
        save.setVerticalTextPosition(JLabel.CENTER);
        save.setHorizontalTextPosition(JLabel.CENTER);
        save.setBounds(230,385,120,40);
        //将所有组件添加到容器
        contentPane.add(textArea);
        contentPane.add(areaBg);
        contentPane.add(save);
        //设置图标、标题和背景颜色
        this.setIconImage(new ImageIcon("resources/images/icon.png").getImage());
        this.setTitle("新增随机池");
        this.setBackground(Color.GRAY);
        this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        //按钮点击处理 lambda表达式
        save.addActionListener((e) -> {
            onButtonOk();
        });
    }

    //事件处理
    private void onButtonOk() {
        String info = textArea.getText();//获取输入内容
        info = info.replaceAll("，",",");
        ArrayList<String> data = new ArrayList<>();
        setData(data);
        Properties cache = new Properties();
        setPropertys(cache);
        Map<String ,String> dataMap = new HashMap<>();
        dataMap.put("data0",dataCache.get("data0"));
        dataMap.put("data1",dataCache.get("data1"));
        dataMap.put("data2",dataCache.get("data2"));
        dataMap.put("data3",dataCache.get("data3"));
        dataMap.put("data4",dataCache.get("data4"));
        //新增和更新有不同的处理方法
        if (isUpdate){
            dataMap.put("data"+signal,info);
            MainFrame.dataCache.set("data"+signal,info);
        }else {
            for (int i=0;i<5;i++){
                if (data.get(i).equals("0")){
                    MainFrame.dataCache.set("data"+i,info);
                    dataMap.put("data"+i,info);
                    signal = i;
                    break;
                }
            }
        }
        dataCache.update(dataMap,"DATA.properties");
        //处理完数据后操作主界面的组件
        MainFrame.labels.get(signal).deleteAddBtn();
        MainFrame.labels.get(signal).addEditBtn();
        MainFrame.labels.get(signal).addDeleteBtn();
        MainFrame.labels.get(signal).text.setText("******");
        if (signal < 4 && !isUpdate){
            MainFrame.labels.get(signal+1).addAddBtn();
        }
        MainFrame.refreshFrame();
        isUpdate = false;
        signal = 0;
        this.dispose();
    }

    public void setPropertys(Properties cache){
        cache.setProperty("data0",dataCache.get("data0"));
        cache.setProperty("data1",dataCache.get("data1"));
        cache.setProperty("data2",dataCache.get("data2"));
        cache.setProperty("data3",dataCache.get("data3"));
        cache.setProperty("data4",dataCache.get("data4"));
    }

    public void setData(ArrayList<String> data){
        data.add(dataCache.get("data0"));
        data.add(dataCache.get("data1"));
        data.add(dataCache.get("data2"));
        data.add(dataCache.get("data3"));
        data.add(dataCache.get("data4"));
    }

}
